'use strict';
const {getDefaultModelConfig} = require("../../utils/common");
const { formatDecimal } = require("../../utils/index");

// 每日菜单项表
module.exports = app => {
    const { STRING, INTEGER, BIGINT, DECIMAL, TEXT } = app.Sequelize;
    const DailyMenuItem = app.model.define(
        'DailyMenuItem',
        {
            id: { type: BIGINT, primaryKey: true, autoIncrement: true },
            daily_menu_id: { type: BIGINT, allowNull: false, comment: '每日菜单ID' },
            dish_id: { type: BIGINT, allowNull: false, comment: '菜品ID' },
            base_price: { type: DECIMAL(10, 2), allowNull: false, comment: '官方参考价格', get() {
                return formatDecimal(this.getDataValue('base_price'));
            } },
            supplier_quoted_price: { type: DECIMAL(10, 2), allowNull: false, comment: '供应商月报价', get() {
                return formatDecimal(this.getDataValue('supplier_quoted_price'));
            } },
            planned_quantity: { type: DECIMAL(10, 2), allowNull: false, comment: '计划采购数量', get() {
                return formatDecimal(this.getDataValue('planned_quantity'));
            } },
            actual_price: { type: DECIMAL(10, 2), comment: '实际采购价格, 供应商', get() {
                return formatDecimal(this.getDataValue('actual_price'));
            } },
            actual_quantity: { type: DECIMAL(10, 2), comment: '实际采购数量, 供应商', get() {
                return formatDecimal(this.getDataValue('actual_quantity'));
            } },
            actual_quantity2: { type: DECIMAL(10, 2), comment: '实际采购数量2, 采购员', get() {
                return formatDecimal(this.getDataValue('actual_quantity2'));
            } },
            quality_rating: { type: TEXT, comment: '品质评价' },
            planned_remark:  { type: TEXT, comment: '备注' },
            remark: { type: TEXT, comment: '备注' },
            status: { type: INTEGER, defaultValue: 1, comment: '状态 1:待采购 2:已采购 3:已收货 4:已完成' },
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'daily_menu_item',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    // 定义关联关系
    DailyMenuItem.associate = function() {
        // 与每日菜单的关联
        DailyMenuItem.belongsTo(app.model.Business.DailyMenu, {
            foreignKey: 'daily_menu_id',
            as: 'dailyMenu'
        });

        // 与菜品的关联
        DailyMenuItem.belongsTo(app.model.Business.Dish, {
            foreignKey: 'dish_id',
            as: 'dish'
        });
    };

    return DailyMenuItem;
};
